layui.use(["jquery","layer","form",'upload','element'],function () {
    var $ = layui.jquery;
    var layer = layui.layer;
    var form = layui.form;
    var upload = layui.upload;
    var element = layui.element;
    var user = JSON.parse(localStorage.getItem('user'));
    var userId = user.data.userId;

    classify = function classify(){
        layer.open({
            type:2,
            title:"选择分类",
            area:["500px","450px"],
            offset:['180px','550px'],
            content:"product-classify.html",
        });
    }
    $(document).on("mouseenter mouseleave", ".file-iteme", function (event) {
        if (event.type === "mouseenter") {
            //鼠标悬浮
            $(this).children(".handle").fadeIn("fast");
        } else if (event.type === "mouseleave") {
            //鼠标离开
            $(this).children(".handle").hide();
        }
    });
    var imgFiles;
    var imgIndex;
    var imgUploadCount = 0;
    //上传图片
    upload.render({
        elem: "#upLoad",
        url: "/product/uploadImg",
        field: 'image',
        accept: 'images',
        size: 2048,
        choose: function (obj) {
            imgUploadCount++;
            obj.preview(function(index, file, result){
                imgIndex = index;
                $('#productImg').attr('src',result);
            });
            if(imgUploadCount != 1){
                delete imgFiles[imgIndex];
            }
            imgFiles = obj.pushFile();
            element.progress('imgProgress', '99%');
        },
        done:function (res, index, upload) {
            if (res.data.code == 200) {
                $('#mainImage').val(res.data.src);
                layer.msg('上传图片成功');
                element.progress('imgProgress', '100%');
            }
        }
    });

    var recommend = 3;//新品推荐
    var discount = 3;//可用优惠券
    var ensure = 4;//服务保证
    form.on("radio(recommend)",function (data) {
        if (data.value == "新品"){
            recommend = 2;
        }else {
            recommend = 3;
        }
    });

    form.on("radio(discount)",function (data) {
        if (data.value == "是"){
            discount = 2;
        }else {
            discount = 3;
        }
    });

    form.on("radio(ensure)",function (data) {
        if (data.value == "无忧退货"){
            ensure = 2;
        }else if(data.value =="快速退款") {
            ensure = 3;
        }else{
            ensure = 4;
        }
    });
    //输入验证
    form.verify({
        categroyName: function (value, item) {
            if(value == ''){
                return '请选择分类';
            }
        },
        productName: function (value, item) {
            if(value == ''){
                return '请输入商品名称';
            }
        },
        mainImage: function (value, item) {
            if(value == ''){
                return '请上传图片';
            }
        },
        productDescription: function (value, item) {
            if(value == ''){
                return '请输入内容';
            }
        },
        productLabel: function (value, item) {
            if(value == ''){
                return '请输入商品标签';
            }
        },
        searchKeywords: function (value, item) {
            if(value == ''){
                return '请输入关键字';
            }
        },
        productCode: function (value, item) {
            if(value == ''){
                return '请输入编号';
            }
            var regPos = /^[0-9]+.?[0-9]*$/; //非负浮点数
            if(!regPos.test(value)){
                return '编号必须是数字';
            }
        },
        currentpriceProduct: function (value, item) {
            if(value == ''){
                return '请输入价格';
            }
            var regPos = /^[0-9]+.?[0-9]*$/; //非负浮点数
            if(!regPos.test(value)){
                return '价格必须是数字';
            }
        }
    });
    form.on('submit(sub)',function () {
        var mainImage = $('input[name=mainImage]').val();
        var categroyId = $('input[name=categroyId]').val();
        var productName = $('input[name=productName]').val();
        var productDescription = $('#productDescription').val();
        var productLabel = $('#productLabel').val();
        var productCode = $('input[name=productCode]').val();
        var currentpriceProduct = $('input[name=currentpriceProduct]').val();
        var originalpriceProduct = $('input[name=originalpriceProduct]').val();
        var searchKeywords = $('input[name=searchKeywords]').val();
        $.ajax({
            url: '/product/getProductCode',
            type: 'post',
            dataType: 'json',
            data: {'productCode': productCode},
            success: function (data) {
                var product = data.data;
                if (product == 0) {
                    var param = {
                        "categroyId": categroyId,
                        "productName": productName,
                        "productDescription": productDescription,
                        'productLabel': productLabel,
                        'productCode': productCode,
                        "currentpriceProduct": currentpriceProduct,
                        "originalpriceProduct": originalpriceProduct,
                        "searchKeywords": searchKeywords,
                        "mainImage": mainImage,
                        "recommend": recommend,
                        "discount": discount,
                        "ensure": ensure,
                        'createId': userId
                    };
                    $.ajax({
                        url: "/product/addProduct",
                        data: param,
                        type: 'post',
                        dataType: 'json',
                        success: function (data) {
                            $.ajax({
                                url: "/product/getProductId",
                                data: {"productCode": productCode},
                                type: 'post',
                                dataType: 'json',
                                success: function (data) {
                                    addLog();
                                    addExamine(data.data.productId);
                                }
                            })
                        }
                    })
                }else if(product != 0){
                    layer.msg("已存在商品编号");
                }
            }
        })
    });
    //添加操作日志
    function addLog(){
        var param = {
            'tableName':'js_product',
            'type':2,
            'userId':userId,
            'remark':'添加反馈统计',
            'logType':1,
        };
        $.ajax({
            url:"/product/addProductLog",
            type:'post',
            dataType:'json',
            data:param,
            success:function (data) {
            }
        });
    }
    //添加审核
    function addExamine(productId) {
        var examine = {
            'courseId': productId,
            'launchId': userId,
            'result': 2,
            'remarks': '添加商品 等待审核',
            'type': 3
        }
        $.ajax({
            url: "/product/addCourseRecord",
            data: examine,
            type: 'post',
            dataType: 'json',
            success: function (data) {
                layer.msg("添加成功，待审核");
                setTimeout(function () {
                    location.reload();
                },2000)
            }
        })
    }
    //刷新
    $('#refresh').click(function () {
        location.reload();
    })
    //返回
    $('#goBack').click(function () {
        parent.tabParentChange(3);
    })
    $(document).click(function () {
        parent.$('.rightMenu').hide();
    });
})